#include <iostream>
#include <string>

using namespace std;

int ntest;
string s, p;

int main()
{
	scanf("%d\n", &ntest);
	while (0 < ntest--)
	{
		getline(cin, s);
		int p1 = s.find_first_of('<');
		int p2 = s.find_first_of('>');
		int p3 = s.find_last_of('<');
		int p4 = s.find_last_of('>');
		int p5 = s.size();
		string s2 = s.substr(p1+1, p2-p1-1);
		string s3 = s.substr(p2+1, p3-p2-1);
		string s4 = s.substr(p3+1, p4-p3-1);
		string s5 = s.substr(p4+1, p5-p4-1);
		s.erase(p4, 1);
		s.erase(p3, 1);
		s.erase(p2, 1);
		s.erase(p1, 1);
		cout << s << endl;
		
		getline(cin, p);
		int p6 = p.find_first_of("...");
		p.erase(p6, 3);
		p.insert(p6, s5);
		p.insert(p6, s2);
		p.insert(p6, s3);
		p.insert(p6, s4);
		cout << p << endl;
	}
	return 0;
}
